package servlets;

import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import model.Prevoznik;
import model.User;
import app.PMF;

@SuppressWarnings("serial")
public class ProfileServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		resp.setContentType("text/html; charset=utf-8");

		HttpSession session = req.getSession(true);

		// za prikaz podataka o prevozniku
		String showData = req.getParameter("showData");
		// za izmenu podataka o prevozniku
		String change = req.getParameter("change");
		// ako imamo proslednje nove podake
		String changed = req.getParameter("changed");

		String naziv = req.getParameter("naziv");
		String adresa = req.getParameter("adresa");
		String eMail = req.getParameter("eMail");
		String telefon = req.getParameter("telefon");

		User user = (User) session.getAttribute("user");
		Prevoznik prevoznik = (Prevoznik) session.getAttribute("prevoznik");

		try {
			// ako imamo user objekat u sesiji
			if (user != null) {

				// ako trebamo prikazati povlasticu
				if (showData != null && showData.equals("true")) {

					System.out.println("Ucitavamo prevoznika");
					// Prevoznik prevoznik = Util.getPrevoznikByUser(user);
					// session.setAttribute("prevoznik", prevoznik);

					// redirektovacemo na stranicu za izmenu povlastice
					RequestDispatcher disp = req
							.getRequestDispatcher("WebContent/showProfile.jsp");
					disp.forward(req, resp);
					return;
				}
				// ako trebamo ici na izmenu,
				if (change != null && change.equals("true")) {

					// System.out.println("Ucitavamo prevoznika");
					// Prevoznik prevoznik = Util.getPrevoznikByUser(user);
					// session.setAttribute("prevoznik", prevoznik);

					// redirektovacemo na stranicu za izmenu povlastice
					RequestDispatcher disp = req
							.getRequestDispatcher("/WebContent/changeProfile.jsp");
					disp.forward(req, resp);
					return;
				}
				// ako trebamo izmeniti podatke
				if (changed != null && changed.equals("true")) {

					if (naziv != null && adresa != null && eMail != null
							&& telefon != null) {

						PersistenceManager pm = PMF.get()
								.getPersistenceManager();

						System.out.println("Izmena prevoznika");
						prevoznik.setNaziv(naziv);
						prevoznik.setAdresa(adresa);
						prevoznik.seteMail(eMail);
						prevoznik.setTelefon(telefon);

						// snimamo novog prevoznika
						pm.makePersistent(prevoznik);
						System.out.println("Novi prevoznik: " + prevoznik);

						// redirektovacemo na pocetnu stranicu
						RequestDispatcher disp = req
								.getRequestDispatcher("/timeTableServlet");
						disp.forward(req, resp);

						session.setAttribute("prevoznik", prevoznik);
						pm.close();

						return;
					} else {
						// nepravilno uneti podaci, ispisati gresku
						RequestDispatcher disp = req
								.getRequestDispatcher("WebContent/changeProfile.jsp");
						disp.forward(req, resp);
						return;

					}
				}

			} else {
				System.out.println("addNewBenefitServlet: user == null");
				RequestDispatcher disp = req
						.getRequestDispatcher("/WebContent/logIn.jsp");
				// redirektovacemo na login stranicu
				disp.forward(req, resp);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

}
